import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Main from '../views/Main.vue'
import Property from '../views/Property.vue'
import Store from '../views/Store.vue'
import Warehouse from '../views/Warehouse.vue'

import ShowGoods from '../components/ShowGoods.vue'
import GoodsShow from '../views/GoodsShow.vue'
import bigSort from '../views/bigSort.vue'
import index from '../views/index.vue'

import smallSort from '../views/smallSort.vue'
import brand from '../views/brand.vue'

Vue.use(VueRouter)

const routes = [{
        path: '/',
        name: 'Login',
        component: Login
    }, {
        path: '/main',
        name: 'Main',
        component: Main,
        children: [
            { path: '/main/bigSort', name: 'bigSort', component: bigSort },
            { path: '/main/index', name: 'index', component: index },
            {
                path: "/main/channelShow",
                component: () =>
                    import ('../views/ChannelShow.vue')
            },
            { path: '/main/smallSort', name: 'smallSort', component: smallSort },
            { path: '/main/brand', name: 'brand', component: brand },
            {
                path: '/main/warehouse',
                name: 'Warehouse',
                component: Warehouse
            },
            {
                path: '/main/property',
                name: 'Property',
                component: Property
            },
            {
                path: '/main/store',
                name: 'Store',
                component: Store
            },
            {
                path: '/main/showGoods',
                name: 'ShowGoods',
                component: ShowGoods
            },
            {
                path: '/main/goodsShow',
                name: 'GoodsShow',
                component: GoodsShow
            }
        ]
    },
    {
        path: '/about',
        name: 'About',
        component: () =>
            import ( /* webpackChunkName: "about" */ '../views/About.vue')
    }

]

const router = new VueRouter({
    routes
})
var indexUrl = null;
router.beforeEach((to, from, next) => {
    // console.log(to.fullPath);
    // console.log("to from");
    // console.log(from.fullPath);
    if (to.fullPath == '/') {
        next();
    } else {
        let jwt = localStorage.getItem("jwt");
        if (null != jwt) {
            if (to.fullPath != '/main/index') {
                indexUrl = to.fullPath
            } else if (indexUrl != null && from.fullPath != '/main/index') {
                indexUrl = null;
                router.push({
                    name: 'index',
                    params: { urlPath: from.fullPath }
                });
            }
            next();
        } else {
            if (from.fullPath == '/') {
                return;
            }
            router.push("/");
        }
    }
});

export default router